/**
 * @min Number
 * @max Number
 * */
export function getRandomNum(min, max) {
	const num = Math.random() * (max - min + 1) + (min);
	const randomNum = Math.floor(num);
	return randomNum;
}


export class GL {
	/**
	 * 构造函数
	 * @param {object} opt
	 * @param {number} opt.min 最小整数值
	 * @param {number} opt.max 最大整数值
	 * @param {Map} opt.fenpei 自定义概率 
	 */
	constructor({
		min,
		max,
		fenpei = new Map()
	}) {
		this.min = min;
		this.max = max;
		this.fenpei = fenpei;
	}

	/**
	 * 可分配百分比
	 */
	get baifenbi() {
		return (1 - this.peizhi) / (this.max - this.min - this.fenpei.size);
	}

	/**
	 * 匹配百分比
	 */
	get peizhi() {
		let result = 0;
		for (let i of this.fenpei.values()) {
			if (0 < i && i < 1) result += i;
		}
		return result;
	}


	/**
	 * 随机开奖数
	 * @returns  
	 */
	random(ratio) {
		let sum = 0;
		for (let i = 0; i < ratio.length; i++) {
			sum += ratio[i];
		}
		var ratioArray = ratio;
		ratioArray.sort(function(a, b){return a - b});
		let r = Math.ceil(Math.random() * sum);
		var finds = this.findCloseNum(ratioArray, r);
		var find = 0;
		for (let i = 0; i < ratioArray.length; i++) {
			if(ratioArray[i]==0){
				continue;
			}else{
			 if (r-finds<=finds && finds <= ratioArray[i]) {
			 	find = ratioArray[i];
			 	return find;
			 } else if (i == ratioArray.length - 1) {
			 	find = ratioArray[i];
			 	return find;
			 }	
			}
		}
		return false;
	}
	
	/**
	 * 目标函数最相近的数值
	 * @param {Object} arr
	 * @param {Object} num
	 */
	findCloseNum(arr, num) {
		var index = 0; // 保存最接近数值在数组中的索引
		var d_value = Number.MAX_VALUE; // 保存差值绝对值，默认为最大数值
		for (var i = 0; i < arr.length; i++) {
			var new_d_value = Math.abs(arr[i] - num); // 新差值
			if (new_d_value <= d_value) { // 如果新差值绝对值小于等于旧差值绝对值，保存新差值绝对值和索引
				if (new_d_value === d_value && arr[i] < arr[index]) { // 如果数组中两个数值跟目标数值差值一样，取大
					continue;
				}
				index = i;
				d_value = new_d_value;
			}
		}
		//return d_value;
		return arr[index] // 返回最接近的数值
	}
}
